package com.sltx.service.impl;


import com.sltx.dao.aaa.TaskMapper;
import com.sltx.service.TaskService;
import com.sltx.util.PageData;
import com.sltx.util.TxSendSmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;



    @Override
    public Map<String,Object> getModelAndPackage(PageData pd) {
//        String phone = taskMapper.getPhone(pd);
        List<Map<String, Object>> model = taskMapper.getModel();
        List<Map<String, Object>> choosePackage = taskMapper.getChoosePackage();
        Map<String, Object> map = new HashMap<>();
//        map.put("phone", phone);
        map.put("model", model);
        map.put("choosePackage", choosePackage);
        return map;
    }

    @Override
    public String addTask(PageData pd) {
        List<Map<String,Object>> name = taskMapper.getTaskName(pd); //查看任务表是否有重名
        for(int q = 0;q < name.size();q++){
            if (name.get(q).get("task_name").equals(pd.get("taskName"))){
                return "任务名称重复";
            }
        }
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String taskNum = dateFormat.format(date) + pd.get("user");
        pd.put("taskNum", taskNum);
        pd.put("tjdata", dateFormat1.format(date));
        pd.put("uploaddate", dateFormat1.format(date));
        if (StringUtils.isEmpty(pd.get("userTel"))) {
            pd.put("isnotice", "否");
        } else {
            pd.put("isnotice", "是");
        }
        boolean b = taskMapper.addTask(pd);//把创建任务的信息添加到任务表
        String[] queryCS = pd.get("queryC").toString().split(",");
        String user = pd.get("user").toString();
        boolean b1 = taskMapper.addModel(taskNum,user,queryCS); //把填写的任务关联单位存近任务关联单位表
        List<Map<String,Object>> taskPackageResult =  taskMapper.getTaskPackage(pd);
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < taskPackageResult.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("taskNum", pd.get("taskNum").toString());
            map.put("entname", taskPackageResult.get(i).get("entname"));
            map.put("uniscid", taskPackageResult.get(i).get("uniscid"));
            list.add(map);
        }
        //添加任务中获取到的企业包中的企业
        boolean b2 =  taskMapper.addTaskPackage(list);
        String id = pd.get("id").toString();
        //修改企业包状态
        boolean b3 = taskMapper.updatePackage(id);
        if (!b) {
            return "创建失败";
        } else {
            String taskName = (String) pd.get("taskName");
            TxSendSmsUtil.txSendSms(taskName); //这里调用的传的taskName是{这里面的}
            return "创建成功";
        }
    }

    @Override
    public List<Map<String, Object>> getTaskList(PageData pd) {
        List<Map<String, Object>> result = taskMapper.getTaskList(pd);
        for (int i = 0; i < result.size(); i++) {
            if (StringUtils.isEmpty(result.get(i).get("ent_new"))) {
                result.get(i).put("ent_new", "-");
            }
        }
        return result;
    }

    @Override
    public int getNum(PageData pd) {
        return taskMapper.getNum(pd);
    }

    @Override
    public Map<String,Object> getTaskDetailsUnfinished(PageData pd) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map1 = taskMapper.getTaskInformation(pd);//select DISTINCT task_name,task_num,user_name from f_gqct_task where task_num = #{taskNum}
        String taskName = (String) map1.get("task_name");
        String taskNum = (String) map1.get("task_num");
        String userName = (String) map1.get("user_name");
        map.put("task_name", taskName);
        map.put("task_num", taskNum);
        map.put("user_name", userName);
        List<Map<String,Object>> map2 = taskMapper.getModelInformation(pd);// select DISTINCT query_c from f_gqct_task_model where task_num =#{taskNum}
        List<Map<String, Object>> map3 = taskMapper.getTaskEnterpriseInformation(pd);//select DISTINCT entname,uniscid from f_gqct_task_ent where task_num =#{taskNum}
        List<Map<String,Object>> map4 = taskMapper.getTaskData(pd);
        for (int i = 0; i < map4.size(); i++) {
            if (StringUtils.isEmpty(map4.get(i).get("match_date"))) {
                map4.get(i).put("match_date", "-");
            }
            if (StringUtils.isEmpty(map4.get(i).get("audit_date"))) {
                map4.get(i).put("audit_date", "-");
            }
            if (StringUtils.isEmpty(map4.get(i).get("finish_date"))) {
                map4.get(i).put("finish_date", "-");
            }

        }
        map.put("ModelInformation", map2);
        map.put("TaskEnterpriseInformation", map3);
        map.put("TaskData", map4);
        map.put("TaskCount", "请等待数据匹配完成");
        return map;
    }

    @Override
    public Map<String, Object> getTaskDetails(PageData pd) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map1 = taskMapper.getTaskInformation(pd);//根据taskNum
        String taskName = (String) map1.get("task_name");
        String taskNum = (String) map1.get("task_num");
        String userName = (String) map1.get("user_name");
        map.put("task_name", taskName);
        map.put("task_num", taskNum);
        map.put("user_name", userName);
        List<Map<String,Object>> map4 = taskMapper.getTaskData(pd);
        List<Map<String,Object>> map2 = taskMapper.getModelInformation(pd);
        List<Map<String, Object>> list = new ArrayList<>();
        HashMap<String, Object> findMap = new HashMap<>();
        HashMap<String, Object> findMap1 = new HashMap<>();
        int modelAllCount = taskMapper.getModelAllCount(pd);
        findMap.put("findEnterprise", "匹配企业");
        findMap.put("num", modelAllCount);
        list.add(findMap);
        int modelNoCount = taskMapper.getModelNoCount(pd);
        findMap1.put("noFindEnterprise","未匹配企业");
        findMap1.put("num", modelNoCount);
        list.add(findMap1);
        for (int i = 0; i < map2.size(); i++) {
            Map<String, Object> forMap = new HashMap<>();
            String queryC = (String) map2.get(i).get("query_c");
            pd.put("queryc", queryC);
            int modelResultCount = taskMapper.getModelResultCount(pd);
            forMap.put("queryC", queryC);
            forMap.put("num", modelResultCount);
            list.add(forMap);
        }
        List<Map<String, Object>> map3 = taskMapper.getTaskEnterpriseInformationOver(pd);
        List<Map<String,Object>> list1 = taskMapper.getindustrycod(pd);
        List<String> list2 = new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            String industrycoD = (String) list1.get(i).get("industryco_d");
            list2.add(industrycoD);
        }
        map.put("ModelInformation", map2);
        map.put("TaskEnterpriseInformation", map3);
        map.put("TaskData", map4);
        map.put("TaskCount", list);
        map.put("hangyedalei", list2);
        return map;
    }


    @Override
    public Map<String, Object> getTaskCompaniesList(PageData pd) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        if (pd.get("industrycod").equals("所有")) {
            pd.put("industrycod", "");
        }
        if (pd.get("queryc").equals("所有")) {
            pd.put("queryc", "");
        }
        if (pd.get("status").equals("0") || pd.get("status").equals("2")){
            String entname = (String) pd.get("entname");
            list = taskMapper.getTask1(entname);
        }else if (pd.get("status").equals("3")){
            list = taskMapper.getTaskCompaniesList(pd);
        }
        map.put("TaskEnterpriseInformation", list);
        return map;
    }


    @Override
    public Map<String, Object> getEquityRelationship(PageData pd) {
        return taskMapper.getEquityRelationship(pd);
    }

    @Override
    public List<Map<String, Object>> downloadEnterprise(PageData pd) {
        List<Map<String,Object>> result = taskMapper.downloadEnterprise(pd);
        return result;
    }

    @Override
    public List<Map<String,Object>> DownLoad(PageData pd) {
        List<Map<String,Object>> list = taskMapper.DownLoad(pd);
        return list;
    }
    @Override
    public void UpdateSuspend(PageData pd) {

        if (pd != null){
            taskMapper.UpdateSuspend(pd);
        }
    }

    @Override
    public String deleteGQGX(PageData pd) {
        try {
            taskMapper.deleteGqctResult(pd);
            taskMapper.deleteGqctTask(pd);
            taskMapper.deleteGqctTaskEnt(pd);
            taskMapper.deleteGqctTaskModel(pd);
            taskMapper.deleteGrctGql(pd);
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }



    @Override
    public Map<String, Object> BatchDownload2(PageData pd) {
        return taskMapper.BatchDownload2(pd);
    }

    @Override
    public String getName(PageData pd) {
        return taskMapper.getName(pd);
    }

    @Override
    public String getName1(String nameId) {
        return taskMapper.getName1(nameId);
    }

}
